﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using DO.Workflows;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace DAL.Workflows
{
    public class WorkflowTriggerDALC
    {
        public static void InsertWorkflowTrigger(Guid workflowID, WorkflowTrigger trigger)
        {
            Database db = DatabaseFactory.CreateDatabase("Workflow");
            WorkflowTriggerInstructionBase instruction =
                (WorkflowTriggerInstructionBase) trigger.Instruction;
            db.ExecuteNonQuery("WorkflowTriggerInsert",
                               trigger.ComponentID,
                               workflowID,
                               (int)trigger.TriggerType,
                               instruction.IsActive,
                               instruction.ActivatedOn,
                               instruction.Span.ToString(),
                               instruction.LastRunTime,
                               instruction.NextRunTime);
        }

        public static void ResetWorkflowTrigger(WorkflowTrigger trigger)
        {
            Database db = DatabaseFactory.CreateDatabase("Workflow");
            WorkflowTriggerInstructionBase instruction =
                (WorkflowTriggerInstructionBase)trigger.Instruction;
            db.ExecuteNonQuery("WorkflowTriggerUpdate",
                               trigger.ComponentID,
                               instruction.IsActive,
                               instruction.ActivatedOn,
                               instruction.Span.ToString(),
                               instruction.LastRunTime,
                               instruction.NextRunTime);
        }

        public static bool TriggerExistInWorkflow(Guid componentID, Guid workflowID)
        {
            string connStr = ConfigurationManager.ConnectionStrings["Workflow"].ConnectionString;
            using(SqlConnection conn=new SqlConnection(connStr))
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "WorkflowTriggerExists";
                cmd.Parameters.AddWithValue(
                    "ComponentID", componentID.ToString());
                cmd.Parameters.AddWithValue(
                    "WorkflowID", workflowID.ToString());
                SqlParameter existParam = new SqlParameter()
                                              {
                                                  ParameterName = "Exists",
                                                  Direction = ParameterDirection.Output,
                                                  DbType=DbType.Boolean
                                              };
                cmd.Parameters.Add(existParam);
                cmd.ExecuteNonQuery();
                bool exists = bool.Parse(existParam.Value.ToString());
                return exists;
            }
        }

        public static WorkflowTriggerInstructionBase GetWorkflowTrigger(Guid componentID)
        {
            Database db = DatabaseFactory.CreateDatabase("Workflow");
            WorkflowTriggerInstructionBase instruction =
                new WorkflowTriggerInstructionBase();
            IDataReader reader = db.ExecuteReader("WorkflowTriggerGet", componentID);
            if(reader.Read())
            {
                instruction.ComponentID=new Guid(reader["ComponentID"].ToString());
                instruction.IsActive = bool.Parse(reader["IsActive"].ToString());
                instruction.TriggerType = (TriggerType)Enum.Parse(
                                                typeof (TriggerType), reader["TriggerType"].ToString());
                instruction.ActivatedOn = DateTime.Parse(reader["ActivatedOn"].ToString());
                if(reader["LastRunTime"]!=DBNull.Value)
                {
                    instruction.LastRunTime = DateTime.Parse(reader["LastRunTime"].ToString());
                }
                if(reader["NextRunTime"]!=DBNull.Value)
                {
                    instruction.NextRunTime = DateTime.Parse(reader["NextRunTime"].ToString());
                }
                
                instruction.Span = TimeSpan.Parse(reader["Span"].ToString());
            }
            reader.Close();

            return instruction;
        }

        public static WorkflowTriggerInstructionBase GetWorkflowTriggerByWorkflow(Guid workflowID)
        {
            Database db = DatabaseFactory.CreateDatabase("Workflow");
            WorkflowTriggerInstructionBase instruction =
                new WorkflowTriggerInstructionBase();
            IDataReader reader = db.ExecuteReader("WorkflowTriggerGetByWorkflow", workflowID);
            if (reader.Read())
            {
                instruction.ComponentID = new Guid(reader["ComponentID"].ToString());
                instruction.IsActive = bool.Parse(reader["IsActive"].ToString());
                instruction.TriggerType = (TriggerType)Enum.Parse(
                                                typeof(TriggerType), reader["TriggerType"].ToString());
                instruction.ActivatedOn = DateTime.Parse(reader["ActivatedOn"].ToString());
                if (reader["LastRunTime"] != DBNull.Value)
                {
                    instruction.LastRunTime = DateTime.Parse(reader["LastRunTime"].ToString());
                }
                if (reader["NextRunTime"] != DBNull.Value)
                {
                    instruction.NextRunTime = DateTime.Parse(reader["NextRunTime"].ToString());
                }
                
                instruction.Span = TimeSpan.Parse(reader["Span"].ToString());
            }
            reader.Close();

            return instruction;
        }

        public static void DeleteWorkflowTrigger(Guid componentID)
        {
            Database db = DatabaseFactory.CreateDatabase("Workflow");
            db.ExecuteNonQuery("DeleteWorkflowTrigger", componentID);
        }
    }
}
